const fs = require('fs');
const { docs } = require('./docs.js');

// https://help.aliyun.com/document_detail/${id}.html
const runDealDocs = () => {
  const needReplace = docs.map((item) => {
    let newLink = item.link;
    if (/^https:\/\/help\.aliyun\.com\/document_detail\/(\d*)\.html$/.test(newLink)) {
      newLink = newLink.replace(
        /^https:\/\/help\.aliyun\.com\/document_detail\/(\d*)\.html$/,
        'aliyunLink($1)',
      );
    }

    item.link = newLink;
    if (typeof item.id === 'object') {
      return {
        id: item.id.id,
        link: item.link,
      };
    }
    return item;
  });

  const logPath = './scripts/docs.txt';
  const text = JSON.stringify(needReplace);

  fs.writeFile(logPath, text, 'utf-8', (err) => {
    if (err) {
      console.log(err);
    } else {
      console.log('生成文件');
    }
  });
};

module.exports = {
  runDealDocs,
};
